package com.wzq.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzq.dao.pojo.Like;
import com.wzq.dao.pojo.PageBean;
import com.wzq.dao.pojo.R;
import com.wzq.service.ILikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yzc
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/admin/like")
public class AdminLikeController extends GetSome {

    @Autowired
    private ILikeService iLikeService;

    @RequestMapping("/list")        //添加
    public R list(@RequestBody PageBean pageBean) {
        String query = pageBean.getQuery().trim();
        Page<Like> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        Page<Like> pageResult = iLikeService.page(page, "".equals(query)?null:
                new QueryWrapper<Like>().in("w_id",searchByWname(query) ));
        Map<String, Object> map = new HashMap<>();
        //        添加用户名称和项目名称
        List<Like> likes = new ArrayList<>();
        for (Like like : pageResult.getRecords()) {
            like.setW_name(getW_name(like.getW_id()));
            like.setP_name(getP_name(like.getP_id()));
            likes.add(like);
        }
        map.put("LikeList", likes);
        map.put("total", pageResult.getTotal());
        return R.ok(map);
    }

    @RequestMapping("/save")        //添加修改
    public R save(@RequestBody Like like) {
        if (like.getId() == null || like.getId() == -1) {
            iLikeService.save(like);
        } else {
            iLikeService.saveOrUpdate(like);
        }
        return R.ok();
    }

    @GetMapping("/{id}")            //单个回显
    public R findById(@PathVariable(value = "id") Integer id) {
        Like like = iLikeService.getById(id);
        //        获得用户名称和项目名称
        like.setP_name(getP_name(like.getP_id()));
        Map<String, Object> map = new HashMap<>();
        map.put("like", like);
        return R.ok(map);
    }

    @GetMapping("/delete/{id}")     //删除
    public R delete(@PathVariable(value = "id") Integer id) {
        boolean b = iLikeService.removeById(id);
        if (b){
            return R.ok("删除成功");
        }
        return R.error("删除失败");
    }
}

